# -*- coding: utf-8 -*-
from typing import Optional

from zinx.ziface.iserver import IServer
import json
import os


class GlobalObj:
    def __init__(self, host: str, tcp_port: int, name: str, version: str, max_packet_size: int, max_conn: int, worker_pool_size: int):
        self.TcpServer: Optional[IServer] = None  # 当前zinx的全局Server对象
        self.Host: str = host  # 当前服务器主机IP
        self.TcpPort: int = tcp_port  # 当前服务器主机监听端口号
        self.Name: str = name  # 当前服务器名称
        self.Version: str = version  # 当前Zinx版本号
        self.MaxPacketSize: int = max_packet_size  # 都需数据包的最大值
        self.MaxConn: int = max_conn  # 当前服务器主机允许的最大链接个数
        self.WorkerPoolSize = worker_pool_size
        self.Reload()

    def Reload(self):
        """
        读取用户的配置文件，覆盖默认配置
        :return:
        """
        root_path = os.getcwd()
        conf_path = "/conf/"
        conf_name = "zinx.json"
        real_path = root_path + conf_path + conf_name
        if not os.path.exists(real_path):
            return

        with open(real_path, 'r', encoding='utf-8') as jsonfile:
            data = json.load(jsonfile)
            for key in data.keys():
                if key in self.__dict__.keys():
                    self.__dict__[key] = data[key]

    def SetTcpServer(self, tcp):
        """
        记录一下TCPServer
        :param tcp:
        :return:
        """
        self.TcpServer = tcp


# 全局对象
GlobalObject = GlobalObj("0.0.0.0", 8986, "ZinxServerApp", "测试版本", 4096, 12000, 10)
# 顺便做一个全局Events
GlobalGevents: list = []
